package com.test.deencredit.analytics_deencredit.util_deencredit

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import androidx.core.content.edit
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.test.deencredit.base_deencredit.BaseApplication_deencredit
import com.test.deencredit.config_deencredit.Constant_deencredit
import java.sql.DriverManager.println

object AdvertIdUtil_deencredit {

    /** sp key gaid */
    const val SP_KEY_GAID_deencredit = "GAID"

    /** sp key gaid enable */
    const val SP_KEY_AD_ENABLE_deencredit = "ENABLE"

    /** 初始化sp */
    private var mSp_deencredit: SharedPreferences = BaseApplication_deencredit.getInstant().getSharedPreferences(
        Constant_deencredit.PRODUCT_deencredit,
        Context.MODE_PRIVATE
    )

    /** 是否允许广告跟踪 */
    private var mAdEnabled_deencredit: Int = 0

    /** 广告id, 每个接口中的advertId字段 */
    private var mAdId_deencredit: String = ""

    init {
        mAdId_deencredit = mSp_deencredit.getString(SP_KEY_GAID_deencredit, "") ?: ""
        mAdEnabled_deencredit = mSp_deencredit.getInt(SP_KEY_AD_ENABLE_deencredit, 0)
    }

    /**
     * 初始化
     *
     * @param context Application
     */
    fun init_deencredit(context: Application) {
        init_deencredit(context) { _, _ -> }
    }

    /**
     * 初始化
     *
     * @param context Application
     * @param success 成功回调
     */
    fun init_deencredit(context: Application, success: (String, Int) -> Unit) {
        if (mAdId_deencredit.isBlank()) {
            Thread {
                try {
                    if (Looper.getMainLooper() != Looper.myLooper()) {
                        val advertisingIdClient = AdvertisingIdClient.getAdvertisingIdInfo(context)
                        val enable = if (advertisingIdClient.isLimitAdTrackingEnabled) 1 else 0
                        mAdId_deencredit = advertisingIdClient.id ?: ""
                        mAdEnabled_deencredit = enable

                        mSp_deencredit.edit {
                            putInt(SP_KEY_AD_ENABLE_deencredit, mAdEnabled_deencredit)
                            putString(SP_KEY_GAID_deencredit, mAdId_deencredit)
                            commit()
                        }
                        println("mAdId=$mAdId_deencredit mIsLimitAdTrackingEnabled=$mAdEnabled_deencredit")

                        if (mAdId_deencredit.isNotBlank()) {
                            Handler(Looper.getMainLooper()).post {
                                //回调通知
                                success.invoke(mAdId_deencredit, mAdEnabled_deencredit)
                            }
                        }
                    } else {
                        println("init GAID ERROR!!! Cannot call in the main thread, You must call in the other thread")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }.start()
        }
    }

    /**
     * 获取广告id
     *
     * @return 广告id
     */
    fun getAdId_deencredit(): String {
        return mAdId_deencredit
    }

    /**
     * 获取是否限制广告跟踪
     *
     * @return 限制状态
     */
    fun getAdEnabled_deencredit(): Int {
        return mAdEnabled_deencredit
    }

    /**
     * 获取安卓id
     *
     * @return 安卓id
     */
    fun getAndroidId_deencredit(): String {
        val androidID = Settings.System.getString(
            BaseApplication_deencredit.getInstant().contentResolver,
            Settings.Secure.ANDROID_ID
        )
        if (androidID.isBlank())
            return System.currentTimeMillis().toString()

        if (androidID.contains("00000000"))
            return System.currentTimeMillis().toString()

        return androidID
    }


}